<?php
include_once "topo.php";
if(logado()){
    $cont_id_busca = 0;
    if($_POST["btn_buscar"] == "Buscar"){
        include_once "controle/busca.php";
    }
    ?>
        <h3>CI's Deferidos</h3>  
        <form action="" method="post">
            Busca: 
            <input type="text" name="busca" id="busca" value="<?=$_POST["busca"]?>">
            <button type="submit" value="Buscar" name="btn_buscar" id="submit">Buscar</button>
        </form>
        <br>
        <table>
            <tr>
                <th></th>
                <th>N&ordm;</th>
                <th>Assunto</th>
                <th>Remetente</th>
                <th>Criado em</th>
                <th>Destinatario</th>
            </tr>
            <?php
            $sql = "SELECT DISTINCT p.id FROM ci_protocolo p";
            if($_SESSION["ci_user_nivel"] == 2){
                $sql .= ", ci_despachante d WHERE (p.id_interessado = '".$_SESSION["ci_user_id"]."' OR p.id_destinatario = '".$_SESSION["ci_user_id"]."')";
            }
            if($cont_id_busca > 0){
                $ids = '';
                for($i = 0; $i < $cont_id_busca;$i++){
                    if($ids != ''){
                        $ids .= ", ";
                    }
                    $ids .= $id_busca[$i];
                }
                if($_SESSION["ci_user_nivel"] == 2){
                    $sql .= " AND p.id IN($ids)";
                }else{
                    $sql .= " WHERE p.id IN($ids)";
                }
            }
            $sql .= " ORDER BY p.id DESC";
            
            $db = new db(config::$driver);
            $conexao = $db->conecta();
            $result = $db->query($sql, $conexao);
            $db->close($conexao);
            $zb = 0;
            while($ids = $db->fetch_array($result)){
                $protocolo = new protocolo();
                $protocolo->carregar($ids[0]);
                
                $data = $protocolo->get_dt_entrada()." ".$protocolo->get_hr_entrada();
                
                $sql_eve = "select id_status from ci_evento where id_protocolo = '".$protocolo->get_id()."' order by id desc limit 1";
                $db1 = new db(config::$driver);
                $conexao = $db1->conecta();
                $res_eve = $db1->query($sql_eve, $conexao);
                $id_status = $db1->fetch_array($res_eve);
                
                if($id_status[0] == 54){
                    
                    $destinatario = $protocolo->get_destinatario();
                    if($protocolo->get_interessado() == $protocolo->get_destinatario()){
                        $sql_dest = "select d.nome from ci_evento e, ci_despachante d where e.id_protocolo = '".$protocolo->get_id()."' and e.id_despachante = d.id order by e.id desc";
                        $res_dest = $db1->query($sql_dest, $conexao);
                        while($dest = $db1->fetch_array($res_dest)){
                            if($dest[0] != $protocolo->get_interessado()){
                                $destinatario = $dest[0];
                                break;
                            }
                        }
                    }
                    
                    if($protocolo->get_arquivo() != ''){
                        $arquivo = "Sim";
                    }else{
                        $arquivo = "Não";
                    }
                    
                    $evento = new evento();
                    $evento->carregar_ultimo($ids[0]);
                    
                    if($evento->get_lido() == 0 && $evento->get_id_destinatario() == $_SESSION["ci_user_id"]){
                        $lido = "Não lido";
                    }else{
                        $lido = "";
                    }
                    
                    if($evento->get_lido() == 0 && $evento->get_id_destinatario() == $_SESSION["ci_user_id"]){
                        $lido = "Não lido";
                    }else{
                        $lido = "";
                    }
                    
                    unset($evento);
                    echo "<tr";
                    if($zb == 0){
                        echo " class='zb'";
                        $zb = 1;
                    }else{
                        echo "";
                        $zb = 0;
                    }
                    
                    if($lido != ''){
                        echo " style='font-weight:bold'";
                    }
                    
                    echo "
                    >
                        <td><a href='verProtocolo.php?id=".$protocolo->get_id()."' title='Alterar'>$lido</a></td>
                        <td><a href='verProtocolo.php?id=".$protocolo->get_id()."' title='Alterar'>".$protocolo->get_id()."</a></td>
                        <td><a href='verProtocolo.php?id=".$protocolo->get_id()."' title='Alterar'>".$protocolo->get_assunto()."</a></td>
                        <td><a href='verProtocolo.php?id=".$protocolo->get_id()."' title='Alterar'>".$protocolo->get_interessado()."</a></td>
                        <td><a href='verProtocolo.php?id=".$protocolo->get_id()."' title='Alterar'>$data</a></td>
                        <td><a href='verProtocolo.php?id=".$protocolo->get_id()."' title='Alterar'>$destinatario</a></td>
                    </tr>
                    ";
                }
                unset($protocolo);
                unset($db1);
            }
            unset($db);
            ?>
        </table>
    <?php
}else{
    header("location: login.php");
}
include_once "rodape.php";
?>